<template>
	<view>
		<view class="uni-list">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value, key) in listData" :key="key" @click="goDetail(value)">
				<view class="uni-media-list">
					<image class="uni-media-list-logo" :src="value.image"></image>

					<view class="uni-media-list-body">
						<view class="uni-media-list-text-top">{{ value.title }}</view>

						<view style="width: 216px;justify-content:space-between;display: flex;">
							<text class="author">{{ value.author }}</text>
							<text>{{ value.category }}</text>
						</view>

						<view class="uni-media-list-text-bottom description">
							<text >{{ value.description }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" :content-text="contentText" />
	</view>
</template>

<script>
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
var dateUtils = require('@/common/util.js').dateUtils;
export default {
	components: {
		uniLoadMore
	},
	data() {
		return {
			listData: [],
			last_id: '',
			reload: false,
			status: 'more',
			contentText: {
				contentdown: '上拉加载更多',
				contentrefresh: '加载中',
				contentnomore: '没有更多'
			}
		};
	},
	onLoad() {
		this.getList();
	},
	onPullDownRefresh() {
		this.reload = true;
		this.last_id = '';
		this.getList();
	},
	onReachBottom() {
		this.status = 'more';
		this.getList();
	},
	methods: {
		getList() {
			var data = {
				column: 'id,title,author,description,image,category' //需要的字段名
			};
			if (this.last_id) {
				//说明已有数据，目前处于上拉加载
				this.status = 'loading';
				data.minId = this.last_id;
				data.time = new Date().getTime() + '';
				data.pageSize = 10;
			}
			uni.request({
				url: 'http://localhost:8080/novels',
				data: data,
				success: data => {
					if (data.statusCode == 200) {
						let list = this.setTime(data.data.body.novels);
						this.listData = this.reload ? list : this.listData.concat(list);
						this.last_id = list[list.length - 1].id;
						this.reload = false;
					}
				},
				fail: (data, code) => {
					console.log('fail' + JSON.stringify(data));
				}
			});
		},

		goDetail: function(e) {
			let detail = {
				author: e.author,
				description: e.description,
				id: e.id,
				image: e.image,
				category: e.category,
				title: e.title
			};
			uni.navigateTo({
				url: '../details/details?detail=' + encodeURIComponent(JSON.stringify(detail))
			});
		},

		setTime: function(items) {
			var novelsItems = [];
			items.forEach(e => {
				novelsItems.push({
					author: e.author,
					description: e.description,
					id: e.id,
					image: e.image,
					category: e.category,
					title: e.title
				});
			});
			return novelsItems;
		}
	}
};
</script>

<style>
.banner {
	height: 360upx;
	overflow: hidden;
	position: relative;
	background-color: #ccc;
}

.banner-img {
	width: 100%;
}

.banner-title {
	max-height: 84upx;
	overflow: hidden;
	position: absolute;
	left: 30upx;
	bottom: 30upx;
	width: 90%;
	font-size: 32upx;
	font-weight: 400;
	line-height: 42upx;
	color: white;
	z-index: 11;
}

.uni-media-list-logo {
	width: 180upx;
	height: 250upx;
}

.uni-media-list-body {
	height: auto;
	justify-content: space-around;
}

.uni-media-list-text-top {
	height: 37upx;
	font-size: 28upx;
	font-weight: 900;
	overflow: hidden;
}

.uni-media-list-text-bottom {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	/* overflow: hidden;
	text-overflow: ellipsis; */
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 5;
	overflow: hidden;
	line-height: 1.1;
}
.author {
	width:130px;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.description{
	text-indent: 25px;
}
</style>
